'''
Created on Apr 1, 2012

@author: Johnathon
'''
from LineValues import Zero
from LineValues import One
from LineValues import X
from LineValues import D
from LineValues import DNot 

class Gate(object):
    '''
    A class that all other gate classes inherit from
    '''

    def __init__(self, gateName, level = -1, inputLines = [], outputLine = None):
        '''
        Constructor
        '''
        self.name = gateName
        self.level = level
        self.inputs = inputLines
        self.output = outputLine
        
    def __repr__(self):
        return "%s (%s): %s" % (self.name, self.level, self.output.value)
    
    def evaluate(self):
        raise NotImplementedError

    def addToDFrontier(self, dFront):
        if self.output.value is X:
            for line in self.inputs:
                if line.value is D or line.value is DNot:
                    dFront.append( (self.output, line.value) )
                    break

    def printSelf(self):
        inputNames = ""
        outputName = ""
        
        if self.output != None:
            outputName = str(self.output.name)
        else: 
            outputName = "Primary Output"

        for count in range(len(self.inputs)):
            inputNames += str(self.inputs[count].name) + ', '

        inputNames = inputNames.strip(', ') 
        print "----------"
        print self.name
        print "Inputs: " + inputNames
        print "Output: " + outputName 
        print "Level: " + str(self.level) 
        print "----------"